import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { ConfigModel } from '#/api/system/models';

import { isUndefined } from '@vben/utils';

import { z } from '#/adapter/form';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      dependencies: {
        triggerFields: ['code'],
        disabled(values) {
          return !isUndefined(values.id);
        },
      },
      fieldName: 'code',
      label: '配置代码',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '配置名称',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'value',
      label: '配置值',
      help: 'Y 表示 true 值',
      rules: z.string().trim().min(1),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      fieldName: 'isFixed',
      label: '内置参数',
      help: '内置参数不能修改和删除',
      defaultValue: false,
      rules: 'required',
    },
    {
      component: 'Switch',
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        class: 'w-auto',
      },
      fieldName: 'status',
      label: '状态',
      defaultValue: 1,
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'remark',
      label: '备注',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<ConfigModel>,
): VxeTableGridOptions<ConfigModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'name', title: '配置名称', width: 220 },
    { field: 'code', title: '配置编码', width: 260 },
    { field: 'value', title: '配置值', width: 160 },
    {
      field: 'isFixed',
      title: '内置参数',
      width: 80,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'green', label: '是', value: true },
          { color: 'yellow', label: '否', value: false },
        ],
      },
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: { name: 'CellTag', attrs: { useStatus: true } },
    },
    { field: 'remark', title: '备注', minWidth: 120 },
    {
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: '职位',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: ['edit', 'delete'],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
